package com.tpl.core.basedata.action;

import java.util.ArrayList;
import java.util.List;

import com.tpl.core.JsonUtil;
import com.tpl.core.action.BaseAction;
import com.tpl.core.basedata.common.constant.ICacheConstants;
import com.tpl.core.basedata.service.AuthService;
import com.tpl.core.basedata.service.DeptService;
import com.tpl.core.basedata.service.DeptUserService;
import com.tpl.core.common.TreeHelper;
import com.tpl.core.common.constant.IConstants;
import com.tpl.core.common.constant.ISessionContants;
import com.tpl.core.entity.SessionUser;
import com.tpl.core.entity.sys.Dept;
import com.tpl.core.entity.sys.Menu;
import com.tpl.core.entity.sys.User;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class MainAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8872874916038428976L;


	@Autowired
	private AuthService authService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private DeptUserService deptUserService;
	
	@RequestMapping("getMenu.do")
	public @ResponseBody void getMenus(String menuNo){
		List<Menu> allList= authService.doGetMenuName(menuNo);
		outJsonSuccess(JsonUtil.toJSONArray(allList).toString());
	}
	
	@RequestMapping("top.do")
	public String toTop(){
		SessionUser user=getLoginUser();
		List menuList= authService.getMenu(ICacheConstants.KEY_AUTH_DEPT_MENU, user);
		putInRequest("menuList", menuList,"user",user);
		return "top";
	}
	@RequestMapping("leftTree.do")
	public void leftTree(final Integer leftIndex){
		SessionUser user=getLoginUser();
		List menuList= authService.getMenu(ICacheConstants.KEY_AUTH_DEPT_MENU, user);
		TreeHelper tree=new TreeHelper(new String[]{"id","menuName","menuNo","parentId","menuType","orderNum","menuUrl"},menuList,3) {
			@Override
			public Object root() {
				return leftIndex;
			}
			
			@Override
			public Object[] idTextData(Object[] nodeData) {
				return new Object[]{nodeData[0],nodeData[1]};
			}
		};
		outJson(tree);
	}
	@RequestMapping("/index.do")
	public String toIndex(){
		SessionUser user=getLoginUser();
		if(user==null)
			return "index";
		else{
			List<Menu> allList= (List<Menu>) authService.getMenu(ICacheConstants.KEY_AUTH_DEPT_MENU, user);
			TreeHelper tree=new TreeHelper(new String[]{"id","menuName","menuNo","parentId","menuType","orderNum","menuUrl","lock","icon"},allList,3) {
				@Override
				public Object root() {
					return -1;
				}
				@Override
				public Object[] idTextData(Object[] nodeData) {
					return new Object[]{nodeData[0],nodeData[1]};
				}
			};
			putInRequest("tree",tree.getTreeJSONArray(),"user",user);
			return "main/index";
		}
	}
	@RequestMapping("/toChangePwd.do")
	public String toEditPassword(){
		SessionUser user=getLoginUser();
		putInRequest("user",user);
		return "system/updatePwd";
	}
	@RequestMapping("/toChangeUserMessage.do")
	public String toChangeUserMessage(){
		Long userId=getLoginUser().getId();
		User user=deptUserService.getUserById(userId);
		putInRequest("user",user);
		return "system/updateUserMessage";
	}
	@RequestMapping("/changePwd.do")
	public @ResponseBody void changetPassword(User user, String newPwd){
		user.setId(getLoginUser().getId());
		authService.doChangePwd(user, newPwd);
		outJsonSuccess();
	}
	/**
	 * 说明 :改变用户登录部门
	 */
	@RequestMapping("/toChangeDept.do")
	public String toChangeDept(){
		SessionUser user=getLoginUser();
		JSONArray ja=authService.getUserDept(user);
		List list=new ArrayList();
		Long curDeptId=user.getDept().getId();
		JSONObject curDept=new JSONObject();
		Long rootId=0L;
		for(int i=0,len=ja.size();i<len;i++){
			JSONObject jo=ja.getJSONObject(i);
			if(curDeptId==jo.getLong("id")){
				jo.put("cur", true);
				//break;
				curDept=jo;
			}else jo.put("cur", false);
			if(jo.containsKey("root")&&jo.getBoolean("root"))
				rootId=jo.getLong("parentId");
			list.add(jo);
		}
		final Long root=rootId;
		TreeHelper tree=new TreeHelper(new String[]{"id","deptName","parentId","cur","defaultFlag"},list,2) {
			
			@Override
			public Object root() {
				return root;
			}
			
			@Override
			public Object[] idTextData(Object[] nodeData) {
				Object text=nodeData[1];
				if(nodeData[3]!=null&&Boolean.valueOf(nodeData[3].toString()))
					text=nodeData[1]+"(当前部门)";
				if(nodeData[4]!=null&&Long.valueOf(nodeData[4].toString())>0)
					text=nodeData[1]+"(默认部门)";
				return new Object[]{nodeData[0],text};
			}
			@Override
			protected void setJsonAttributesData(JSONObject jo, String[] keys, Object[] nodeData) {
				super.setJsonAttributesData(jo, keys, nodeData);
				if(jo.containsKey("cur")&&jo.getBoolean("cur")){
					jo.put("nocheck", true);
				}
			}
		};
		putInRequest("depts", ja.toString(),"treeData",tree.toString(),"curDept",curDept);
		return "main/changeDepts";
	}
	
	/**
	 * 说明 :改变用户登录部门
	 */
	@RequestMapping("/changeDept.do")
	public @ResponseBody void changeDept(Dept dept){
		SessionUser user=getLoginUser();
		dept=deptService.getDeptById(dept.getId());
		user.setDept(dept);
		putInRequest(ISessionContants.KEY_SESSION_USER,user);
		outJsonSuccess();
	}
	@RequestMapping("/changeDefaultDept.do")
	public @ResponseBody void changeDefaultDept(Long deptId){
		SessionUser user=getLoginUser();
		authService.changeDefaultDept(deptId, user.getId());
		//user.setDept(null);
		
		outJsonSuccess();
	}
	
	@RequestMapping("/getLoginUser.do")
	public @ResponseBody void getLoginUser(Long deptId){
		SessionUser user=getLoginUser();
		authService.changeDefaultDept(deptId, user.getId());
		
		outJsonSuccess();
	}

	@RequestMapping("/404")
	public String $404(){
		putInRequest("httpdUrl", IConstants.STATIC_SOURCE_BASE_PATH);
		return "main/404";
	}

	@RequestMapping("/error.do")
	public String error(){
		return "main/error";
	}



}
